package com.iris.live.services.report.sources;

import com.iris.live.services.common.DateFormatUtils;
import com.iris.live.services.common.StringUtils;
import com.iris.live.services.data.repositories.DataCheckRepository;
import com.iris.live.services.report.Cell;
import com.iris.live.services.report.IExcelReportSource;
import com.iris.live.services.report.Row;
import com.iris.live.services.report.Sheet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lusong on 2017/5/23.
 */
@Component("ExportDataCheckFinanceFalseRateReportSource")
public class ExportDataCheckFinanceFalseRateReportSource implements IExcelReportSource {

    private Logger logger = LoggerFactory.getLogger(ExportDataCheckFinanceFalseRateReportSource.class);

    @Autowired
    private DataCheckRepository dataCheckRepository;

    @Override
    public Sheet[] getSheets(Map<String, Object> context) {
        Sheet[] sheets = new Sheet[1];
        sheets[0] = new Sheet("经销商_数据核验_错误率报告_金融", "经销商_数据核验_错误率报告_金融", true);
        return sheets;
    }

    @Override
    public List<Row> getSheetRows(Map<String, Object> context, Sheet sheet) throws ParseException {
        List<Row> values = new ArrayList<>();

        String permit = context.get("permit") == null ? null : context.get("permit").toString();
        String startDate = context.get("exportStartDate") == null ? null : context.get("exportStartDate").toString();
        String endDate = context.get("exportEndDate") == null ? null : context.get("exportEndDate").toString();
        String nowDate = null;
        if (StringUtils.isNullOrBlank(startDate) && StringUtils.isNullOrBlank(endDate)) {
            nowDate = DateFormatUtils.getFormatDateToString(new java.util.Date(), "yyyyMMdd");
        }
        List list = dataCheckRepository.getFinanceFalseRate(permit, nowDate, startDate, endDate);

        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());
        values.add(new Row());

        if (list != null && !list.isEmpty()) {
            for (Object o : list) {
                Map<String, Object> map = (Map) o;
                values.add(new Row(getCells(map)));
            }
        }
        return values;
    }

    private Cell[] getCells (Map<String, Object> map) {
        Cell[] cells = new Cell[13];
        int index = 0;
        cells[index++] = new Cell(map.get("贷款人姓名") == null ? "" : map.get("贷款人姓名").toString(), index);
        cells[index++] = new Cell(map.get("贷款申请日期") == null ? "" : map.get("贷款申请日期").toString(), index);
        cells[index++] = new Cell(map.get("金融机构") == null ? "" : map.get("金融机构").toString(), index);
        cells[index++] = new Cell(map.get("保证金金额") == null ? "" : map.get("保证金金额").toString(), index);
        cells[index++] = new Cell(map.get("首付金额") == null ? "" : map.get("首付金额").toString(), index);
        cells[index++] = new Cell(map.get("贷款金额") == null ? "" : map.get("贷款金额").toString(), index);
        cells[index++] = new Cell(map.get("贷款期数") == null ? "" : map.get("贷款期数").toString(), index);
        cells[index++] = new Cell(map.get("利率_费率") == null ? "" : map.get("利率_费率").toString(), index);
        cells[index++] = new Cell(map.get("厂家贴息金额") == null ? "" : map.get("厂家贴息金额").toString(), index);
        cells[index++] = new Cell(map.get("经销商贴息金额") == null ? "" : map.get("经销商贴息金额").toString(), index);
        cells[index++] = new Cell(map.get("放款到账日") == null ? "" : map.get("放款到账日").toString(), index);
        cells[index++] = new Cell(map.get("金融手续费") == null ? "" : map.get("金融手续费").toString(), index);
        cells[index++] = new Cell(map.get("金融服务费") == null ? "" : map.get("金融服务费").toString(), index);
        return cells;
    }
}
